package edu.uoc.tdp.pac3.server;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.swing.JOptionPane;
import edu.uoc.tdp.pac3.common.tools.TDSLanguageUtils;

/**
 * Clase GestorDisc
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class GestorDisc {

    public static final String SQL_STATE_UNIQUE_VIOLATION = "23505";
    private Connection objConn;
    private TDSLanguageUtils mess = null;

    /**
     * Constructor
     * @throws RemoteException
     */
    public GestorDisc() {
        objConn = null;
    }

    /**
     * Inicialitzem la connexio amb la BD.     
     * @throws RemoteException
     */
    public void initConnection() throws Exception {
        String sUrl = null;
        String sUserName = null;
        String sPassword = null;
        Properties objProperties = new Properties();

        // Propietats
        try {
            FileInputStream objFIS = new FileInputStream("properties/configuration.properties");
            objProperties.load(objFIS);
            sUrl = objProperties.getProperty("url");
            sUserName = objProperties.getProperty("username");
            sPassword = objProperties.getProperty("password");
        } catch (FileNotFoundException e1) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err..notfound") + "\n" + e1.getMessage(),
                    mess.getMessage("bbdd.err"), JOptionPane.ERROR_MESSAGE);
        } catch (IOException e2) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err.notopen") + "\n" + e2.getMessage(),
                    mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
        } catch (Exception eAl) {
            // Altres
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err.unknown") + "\n" + eAl.getMessage(),
                    mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
        }

        //Driver
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err.driver") + "\n" + e.getMessage(),
                    mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
        }

        // Conexio
        try {
            objConn = DriverManager.getConnection(sUrl, sUserName, sPassword);
        } catch (SQLException e) {
            // Error a l'intentar obrir la connexi�
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("con.db.err.open") + "\n" + e.getMessage(),
                    mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Retorna l'objecte de connexio a la BD
     * 
     * @return Connection
     * @throws RemoteException
     */
    public Connection getConnection() {
        return objConn;
    }

    /**
     * Tanca la connexio amb la BD
     * @throws RemoteException
     */
    public void closeConnection() throws Exception {
        if (objConn != null) {
            try {
                objConn.close();
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("con.db.close") + "\n" + e.getMessage(),
                        mess.getMessage("con.db.err"), JOptionPane.ERROR_MESSAGE);
            }
            objConn = null;
        }
    }
}
